<?php

namespace app\admin\controller\car;

use app\common\controller\Backend;
use app\common\model\shopro\CarDetailsModel;
use fast\Http;

/**
 * 车品牌
 *
 * @icon fa fa-circle-o
 */
class Brand extends Backend
{

    /**
     * Brand模型对象
     * @var \app\admin\model\car\Brand
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\car\Brand;

    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    public function car_inquire($ids)
    {
        $car_cache = cache('car_pinpai-'.$ids);
        $appkey    = config('site.car_appkey');
        if ($car_cache) {
            $result['result'] = cache('car_pinpai-'.$ids);
        } else {
            $url    = "https://api.jisuapi.com/car/type?appkey=".$appkey."&parentid=".$ids;
            $result = Http::get($url);
            $result = is_string($result) ? json_decode($result, true) : $result;
            if (isset($result['code']) && $result['code'] != 0) {
                $msg = $result['data']['msg'] ?? ($result['message'] ?? '');
                $this->error($msg);
            }
            cache('car_pinpai-'.$ids, $result['result']);
        }
        if(empty($result['result'])){
            $this->error($result['msg']);
        }
        foreach ($result['result'] as $k => $v) {
            foreach ($v['list'] as $kk => $vv) {
                if ($vv['salestate'] == "在销") {
                    $vv['all_name']     = $v['name'];
                    $vv['car_brand_id'] = $ids;
                    $vv['one_id']       = $vv['id'];
                    $vv['two_id']       = $v['id'];
                    $vv['initial']      = $v['initial'];
                    $ids                = $vv['id'];
                    unset($vv['id']);
                    db('shopro_car_vehicle')->insert($vv);
                    $this->car_info($appkey, $ids);
                }
            }
        }
        $this->success('ok');
    }

    /**
     * @menu 获取车
     * ''
     * @Date:2024-05-21 20:42
     */
    public function car_info($appkey, $parentid)
    {
        $car_cache = cache('car_list-'.$parentid);

        if ($car_cache) {
            $result = $car_cache;
        } else {
            $url    = "https://api.jisuapi.com/car/car?appkey=".$appkey."&parentid=".$parentid."&sort=productionstate";
            $result = Http::get($url);
            $result = is_string($result) ? json_decode($result, true) : $result;
            cache('car_list-'.$parentid, $result['result']);
        }
        if(!empty($result['list'])){
            $sku   = $result['list'];
            $inser = $result;
            $ids   = $result['id'];
            unset($inser['list'], $inser['id']);
            $inser['parentid'] = $parentid;
            $inser['car_ids']  = $ids;
            unset($result['id']);

            $ids = db('shopro_car_list')->insertGetId($inser);
            foreach ($sku as $k => $v) {
                if ($v['salestate'] == '在销') {
                    $car_sku['car_list_id']     = $ids;
                    $car_sku['car_id']          = $v['id'];
                    $car_sku['name']            = $v['name'];
                    $car_sku['logo']            = $v['logo'];
                    $car_sku['price']           = $v['price'];
                    $car_sku['productionstate'] = $v['productionstate'];
                    $car_sku['yeartype']        = $v['yeartype'];
                    $car_sku['salestate']       = $v['salestate'];
                    $car_sku['sizetype']        = $v['sizetype'];
                    $car_sku['data_json']       = json_encode($v, true);
                    db('shopro_car_list_sku')->insert($car_sku);
                    $this->detail($appkey, $v['id']);
                }
            }
        }
    }

    /**
     * @menu 获取详情
     * ''
     * @Date:2024-05-21 20:56
     */

    public function detail($appkey, $carid)
    {
        if (cache('car_details-'.$carid)) {
            $result = cache('car_details-'.$carid);
        } else {
            $url    = "https://api.jisuapi.com/car/detail?appkey=".$appkey."&carid=".$carid;
            $result = Http::get($url);
            $result = is_string($result) ? json_decode($result, true) : $result;
            cache('car_details-'.$carid, $result['result']);
            if(!empty($result['result'])){
                $result = $result['result'];
            }
        }
        if (isset($result['code']) && $result['code'] != 0) {

        }else{
            $model = new CarDetailsModel();
            $model->create(['car_id' => $carid, 'car_details_json' => $result]);
        }

    }

}
